import { CrudOptions, AddReq, DelReq, EditReq, dict, CrudExpose, UserPageQuery, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import { ElMessage, ElMessageBox } from 'element-plus';
import * as api from './api';
import { auth } from "/@/utils/authFunction";

export default function ({ crudExpose }: { crudExpose: CrudExpose }): CreateCrudOptionsRet {
    const pageRequest = async (query: any) => {
        return await api.GetList(query);
    };
    const editRequest = async ({ form, row }: EditReq) => {
        if (row.id) {
            form.id = row.id;
        }
        return await api.UpdateObj(form);
    };
    const delRequest = async ({ row }: DelReq) => {
        return await api.DelObj(row.id);
    };
    const addRequest = async ({ form }: AddReq) => {
        return await api.AddObj(form);
    };

    const exportRequest = async (query: UserPageQuery) => {
        return await api.exportData(query);
    };

    const handleViewStockRecords = async (row: any) => {
        try {
            const res = await api.getStockRecords(row.id);
            if (res.data) {
                ElMessageBox.alert(
                    `<div style="max-height: 400px; overflow-y: auto;">
                        <table style="width: 100%; border-collapse: collapse;">
                            <tr>
                                <th style="border: 1px solid #ddd; padding: 8px;">记录类型</th>
                                <th style="border: 1px solid #ddd; padding: 8px;">数量</th>
                                <th style="border: 1px solid #ddd; padding: 8px;">操作前库存</th>
                                <th style="border: 1px solid #ddd; padding: 8px;">操作后库存</th>
                                <th style="border: 1px solid #ddd; padding: 8px;">备注</th>
                            </tr>
                            ${res.data.map((record: any) => `
                                <tr>
                                    <td style="border: 1px solid #ddd; padding: 8px;">${record.record_type === 'in' ? '入库' : record.record_type === 'out' ? '出库' : '盘点'}</td>
                                    <td style="border: 1px solid #ddd; padding: 8px;">${record.quantity}</td>
                                    <td style="border: 1px solid #ddd; padding: 8px;">${record.before_quantity}</td>
                                    <td style="border: 1px solid #ddd; padding: 8px;">${record.after_quantity}</td>
                                    <td style="border: 1px solid #ddd; padding: 8px;">${record.remark || '-'}</td>
                                </tr>
                            `).join('')}
                        </table>
                    </div>`,
                    '库存记录',
                    {
                        dangerouslyUseHTMLString: true,
                        confirmButtonText: '确定'
                    }
                );
            }
        } catch (error) {
            ElMessage.error('获取库存记录失败');
        }
    };

    return {
        crudOptions: {
            request: {
                pageRequest,
                addRequest,
                editRequest,
                delRequest,
            },
            actionbar: {
                buttons: {
                    export: {
                        show: auth('Material:Export'),
                        text: "导出",
                        title: "导出",
                        click() {
                            return exportRequest(crudExpose.getSearchFormData());
                        }
                    },
                    add: {
                        show: auth('Material:Create'),
                    },
                }
            },
            rowHandle: {
                fixed: 'right',
                width: 200,
                buttons: {
                    view: {
                        type: 'text',
                        order: 1,
                        show: auth('Material:Retrieve')
                    },
                    edit: {
                        type: 'text',
                        order: 2,
                        show: auth('Material:Update')
                    },
                    copy: {
                        type: 'text',
                        order: 3,
                        show: auth('Material:Copy')
                    },
                    remove: {
                        type: 'text',
                        order: 4,
                        show: auth('Material:Delete')
                    },
                    stockRecords: {
                        text: '库存记录',
                        type: 'text',
                        order: 5,
                        show: auth('Material:StockRecords'),
                        click: ({ row }) => handleViewStockRecords(row)
                    }
                },
            },
            columns: {
                name: {
                    title: '原材料名称',
                    type: 'input',
                    search: { show: true },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        helper: {
                            render() {
                                return <div style={"color:blue"}>原材料名称为必填项</div>;
                            }
                        },
                        rules: [{ required: true, message: '原材料名称必填' }],
                        component: {
                            placeholder: '请输入原材料名称',
                        },
                    },
                },
                code: {
                    title: '物料编码',
                    type: 'input',
                    search: { show: true },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '物料编码必填' }],
                        component: {
                            placeholder: '请输入物料编码',
                        },
                    },
                },
                specification: {
                    title: '规格型号',
                    type: 'input',
                    search: { show: true },
                    column: {
                        minWidth: 120,
                    },
                    form: {
                        rules: [{ required: true, message: '规格型号必填' }],
                        component: {
                            placeholder: '请输入规格型号',
                        },
                    },
                },
                unit: {
                    title: '计量单位',
                    type: 'input',
                    search: { show: false },
                    column: {
                        minWidth: 80,
                    },
                    form: {
                        rules: [{ required: true, message: '计量单位必填' }],
                        component: {
                            placeholder: '请输入计量单位',
                        },
                    },
                },
                current_stock: {
                    title: '当前库存',
                    type: 'number',
                    search: { show: false },
                    column: {
                        minWidth: 100,
                        sortable: 'custom',
                    },
                    form: {
                        component: {
                            disabled: true,
                        },
                    },
                },
                minimum_stock: {
                    title: '最低库存',
                    type: 'number',
                    search: { show: false },
                    column: {
                        minWidth: 100,
                    },
                    form: {
                        rules: [{ required: true, message: '最低库存必填' }],
                        component: {
                            placeholder: '请输入最低库存',
                        },
                    },
                },
                maximum_stock: {
                    title: '最高库存',
                    type: 'number',
                    search: { show: false },
                    column: {
                        minWidth: 100,
                    },
                    form: {
                        rules: [{ required: true, message: '最高库存必填' }],
                        component: {
                            placeholder: '请输入最高库存',
                        },
                    },
                },
            },
        },
    };
} 